package Controllers;

import Information.CommentInfo;
import Information.PostInfo;
import Information.UserInfo;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

/**
 * @author Kotori
 */
public class PostCommentController implements Initializable {
    /**
     * 五个评论区的控件：头像、昵称、评论内容、时间
     */
    @FXML
    private ImageView userAvatar1;
    @FXML
    private Label userName1;
    @FXML
    private Label userComment1;
    @FXML
    private Label commentTime1;
    @FXML
    private ImageView userAvatar2;
    @FXML
    private Label userName2;
    @FXML
    private Label userComment2;
    @FXML
    private Label commentTime2;
    @FXML
    private ImageView userAvatar3;
    @FXML
    private Label userName3;
    @FXML
    private Label userComment3;
    @FXML
    private Label commentTime3;
    @FXML
    private ImageView userAvatar4;
    @FXML
    private Label userName4;
    @FXML
    private Label userComment4;
    @FXML
    private Label commentTime4;
    @FXML
    private ImageView userAvatar5;
    @FXML
    private Label userName5;
    @FXML
    private Label userComment5;
    @FXML
    private Label commentTime5;
    /**
     * 翻页键
     */
    @FXML
    private Button lastPageButton;
    @FXML
    private Button nextPageButton;
    /**
     * 评论编辑区和发表
     */
    @FXML
    private TextArea commentEditArea;
    @FXML
    private Button publishCommentButton;

    /**
     * 帖子、当前用户
     */
    private PostInfo postInfo;
    private UserInfo userInfo;
    /**
     * 记录当前第一条评论的位置
     * 如0、5、10等
     */
    private int index = 0;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.readInfo();
        this.initComments();
    }


    /**
     * 读入当前帖子及用户
     */
    public void readInfo() {
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/UserPosts/currentPost.ser"));
                ObjectInputStream oisUser = new ObjectInputStream(new FileInputStream("src/UserPosts/currentUser.ser"))
        ) {
            //得到帖子对象
            postInfo = (PostInfo) ois.readObject();
            //得到用户对象
            userInfo = (UserInfo) oisUser.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新过的评论区写入帖子
     */
    public void writePostInfo() {
        String path = "src/UserPosts/postObjects/" + postInfo.getPostId() + ".ser";
        String currentPath = "src/UserPosts/currentPost.ser";
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
                ObjectOutputStream oosCurrent = new ObjectOutputStream(new FileOutputStream(currentPath))
        ) {
            //写入当前帖子对象
            oos.writeObject(postInfo);
            oosCurrent.writeObject(postInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除页面内容
     */
    public void clearComments() {
        lastPageButton.setVisible(false);
        nextPageButton.setVisible(false);
        //清除控件
        userAvatar1.setImage(null);
        userAvatar2.setImage(null);
        userAvatar3.setImage(null);
        userAvatar4.setImage(null);
        userAvatar5.setImage(null);

        userName1.setText(null);
        userName2.setText(null);
        userName3.setText(null);
        userName4.setText(null);
        userName5.setText(null);

        userComment1.setText(null);
        userComment2.setText(null);
        userComment3.setText(null);
        userComment4.setText(null);
        userComment5.setText(null);

        commentTime1.setText(null);
        commentTime2.setText(null);
        commentTime3.setText(null);
        commentTime4.setText(null);
        commentTime5.setText(null);
    }

    /**
     * 初始化评论区
     */
    public void initComments() {
        //取得评论
        ArrayList<CommentInfo> commentsArray = postInfo.getCommentsArray();
        int size = commentsArray.size();
        //清除该页面内容
        this.clearComments();
        //加载评论
        if (size - index >= 1) {
            loadComment1(commentsArray.get(index));
        }
        if (size - index >= 2) {
            loadComment2(commentsArray.get(index + 1));
        }
        if (size - index >= 3) {
            loadComment3(commentsArray.get(index + 2));
        }
        if (size - index >= 4) {
            loadComment4(commentsArray.get(index + 3));
        }
        if (size - index >= 5) {
            loadComment5(commentsArray.get(index + 4));
        }
        //设置按钮
        if (size - index > 5) {
            nextPageButton.setVisible(true);
        }
        if (index != 0) {
            lastPageButton.setVisible(true);
        }
    }

    /**
     * 加载一条评论
     * @param commentInfo 存储该评论的对象
     */
    public void loadComment1(CommentInfo commentInfo) {
        //用户信息
        userAvatar1.setImage(new Image(commentInfo.getUserInfo().getAvatar()));
        userName1.setText(commentInfo.getUserInfo().getPname());
        //评论
        userComment1.setText(commentInfo.getContent());
        //时间
        commentTime1.setText(commentInfo.getCommentTime());
    }

    public void loadComment2(CommentInfo commentInfo) {
        //用户信息
        userAvatar2.setImage(new Image(commentInfo.getUserInfo().getAvatar()));
        userName2.setText(commentInfo.getUserInfo().getPname());
        //评论
        userComment2.setText(commentInfo.getContent());
        //时间
        commentTime2.setText(commentInfo.getCommentTime());
    }

    public void loadComment3(CommentInfo commentInfo) {
        //用户信息
        userAvatar3.setImage(new Image(commentInfo.getUserInfo().getAvatar()));
        userName3.setText(commentInfo.getUserInfo().getPname());
        //评论
        userComment3.setText(commentInfo.getContent());
        //时间
        commentTime3.setText(commentInfo.getCommentTime());
    }

    public void loadComment4(CommentInfo commentInfo) {
        //用户信息
        userAvatar4.setImage(new Image(commentInfo.getUserInfo().getAvatar()));
        userName4.setText(commentInfo.getUserInfo().getPname());
        //评论
        userComment4.setText(commentInfo.getContent());
        //时间
        commentTime4.setText(commentInfo.getCommentTime());
    }

    public void loadComment5(CommentInfo commentInfo) {
        //用户信息
        userAvatar5.setImage(new Image(commentInfo.getUserInfo().getAvatar()));
        userName5.setText(commentInfo.getUserInfo().getPname());
        //评论
        userComment5.setText(commentInfo.getContent());
        //时间
        commentTime5.setText(commentInfo.getCommentTime());
    }

    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(this.getClass().getResource("../Resources/PostComment.fxml"));
            Scene scene = new Scene(root, 600, 800);

            primaryStage.setScene(scene);
            primaryStage.getIcons().add(new Image("/Images/logoblue.png"));
            primaryStage.setTitle("评论");
            primaryStage.setResizable(false);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void lastPage(ActionEvent actionEvent) {
        index = index - 5;
        this.initComments();
    }

    public void nextPage(ActionEvent actionEvent) {
        index = index + 5;
        this.initComments();
    }

    public void publishComment(ActionEvent actionEvent) {
        //获取文字
        String commentText = commentEditArea.getText();
        //获取完文字后删除编辑区内容
        commentEditArea.setText(null);
        //新建评论对象
        CommentInfo commentInfo = new CommentInfo(userInfo, commentText);
        //更新帖子
        postInfo.getCommentsArray().add(commentInfo);
        postInfo.getCommentedByUserID().add(userInfo.getId());
        postInfo.setComments(postInfo.getCommentsArray().size());
        //存入帖子
        this.writePostInfo();
        //刷新评论区界面
        this.readInfo();
        this.initComments();
    }
}
